<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<title>授权</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="__PUBLIC__/layuicms/layui2.76/css/layui.css" media="all" />
	<link rel="stylesheet" href="__PUBLIC__/layuicms/css/public.css" media="all" />
	<style>
		.border-box {
			border: 1px solid #eee;
			padding: 10px 10px 10px 0
		}

		.disabled {
			background: #eee
		}
	</style>
</head>

<body class="childrenBody">

	<div class="layui-form layui-row layui-col-space10" id="form">
		<div class="layui-col-md6 layui-col-xs12 border-box">
			<div id="nav_tree"></div>

			<hr class="layui-bg-gray" />
			<div class="layui-right">
				<input type="hidden" name="is_del" value="0">
				<a class="layui-btn layui-btn-sm" lay-filter="add-btn" lay-submit><i
						class="layui-icon">&#xe609;</i>提交</a>
			</div>
		</div>
	</div>
	<script type="text/javascript" src="__PUBLIC__/layuicms/layui2.76/layui.js"></script>
	<script type="text/javascript" src="__PUBLIC__/admin/js/lib/public.js"></script>
	<script>
		layui.use(['form', 'layer', 'tree'], function () {
			var form = layui.form;
			var layer = parent.layer === undefined ? layui.layer : top.layer;
			var tree = layui.tree;
			var $ = layui.$;

			// 数据
			var data = <php>echo $json_nav;</php>;
			var post_url = "{:U('auth')}";
			var rid = "{$rid}";
			var auth = "{$role.auth}";

			// 渲染树
			tree.render({
				elem: '#nav_tree'
				, data: data
				, showCheckbox: true  //是否显示复选框
				, id: 'nav_tree1'
				, isJump: false //是否允许点击节点时弹出新窗口跳转
				, click: function (obj) {
					var data = obj.data;  //获取当前点击的节点数据
					layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
				}
			});

			// console.log(auth.split(","));
			// 执行勾选
			tree.setChecked('nav_tree1', auth.split(","));

			// 数据提交
			form.on("submit(add-btn)", function () {
				//获得选中的节点
				var checkData = tree.getChecked('nav_tree1');
				var ids = getAllIds(checkData);
				//弹出loading
				var index = top.layer.msg('数据提交中，请稍候', { icon: 16, time: false, shade: 0.8 });
				// 实际使用时的提交信息
				setTimeout(function () {
					$.ajax({
						url: post_url,
						data: { ids: ids ,rid: rid},
						type: "POST",
						dataType: "json",
						success: function (res) {
							if (res.code == 0) {
								top.layer.msg(res.message);
								location.reload();
							} else {
								layer.msg(res.message, { icon: 5 });
							}
						},
						error: function (data) {
							console.log(data);
						}
					});
				}, 500);
			})


		});

		// 获取勾选id
		function getAllIds(arr) {
			var ids = [];

			for (var i = 0; i < arr.length; i++) {
				var obj = arr[i];

				if (obj && obj.id) {
					ids.push(obj.id);
				}

				if (obj && obj.children && obj.children.length > 0) {
					ids = ids.concat(getAllIds(obj.children));
				}
			}

			return ids.join(','); // 将数组中的元素连接成一个字符串，使用逗号分隔
		}
	</script>
</body>

</html>